<?php

namespace common\models\c2\entity;

use common\helpers\Tool;
use cza\base\models\statics\EntityModelStatus;
use Yii;
use yii\helpers\Json;

/**
 * This is the model class for table "{{%proprietor}}".
 *
 * @property integer $id
 * @property integer $id2
 * @property integer $community_id
 * @property integer $building_number_id
 * @property integer $unit_id
 * @property integer $houses_id
 * @property string $name
 * @property string $phone
 * @property string $sy_validity_start
 * @property string $sy_validity_end
 * @property string $remarks
 * @property string $gender
 * @property string $country
 * @property string $nation
 * @property string $birthday
 * @property integer $id_type
 * @property string $id_number
 * @property string $validity_start
 * @property string $validity_end
 * @property string $id_address
 * @property string $Issuing_authority
 * @property integer $hukou
 * @property string $work_unit
 * @property string $bound_vehicle
 */
class Proprietor extends \cza\base\models\ActiveRecord
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%proprietor}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['community_id', 'building_number_id', 'unit_id', 'houses_id', 'id_type', 'hukou'], 'integer'],
            [['name', 'phone', 'sy_validity_start', 'sy_validity_end'], 'required'],
            [['sy_validity_start', 'sy_validity_end', 'birthday', 'validity_start', 'validity_end'], 'safe'],
            [['name', 'phone', 'remarks', 'gender', 'country', 'nation', 'id_address', 'Issuing_authority', 'work_unit', 'bound_vehicle', 'id_number'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app.c2', 'ID'),
            'community_id' => Yii::t('app.c2', 'Community ID'),
            'building_number_id' => Yii::t('app.c2', 'Building Number ID'),
            'unit_id' => Yii::t('app.c2', 'Unit ID'),
            'houses_id' => Yii::t('app.c2', 'Houses ID'),
            'name' => Yii::t('app.c2', 'Name'),
            'phone' => Yii::t('app.c2', 'Phone'),
            'sy_validity_start' => Yii::t('app.c2', 'Sy Validity Start'),
            'sy_validity_end' => Yii::t('app.c2', 'Sy Validity End'),
            'remarks' => Yii::t('app.c2', 'Remarks'),
            'gender' => Yii::t('app.c2', 'Gender'),
            'country' => Yii::t('app.c2', 'Country'),
            'nation' => Yii::t('app.c2', 'Nation'),
            'birthday' => Yii::t('app.c2', 'Birthday'),
            'id_type' => Yii::t('app.c2', 'Id Type'),
            'id_number' => Yii::t('app.c2', 'Id Number'),
            'validity_start' => Yii::t('app.c2', 'Validity Start'),
            'validity_end' => Yii::t('app.c2', 'Validity End'),
            'id_address' => Yii::t('app.c2', 'Id Address'),
            'Issuing_authority' => Yii::t('app.c2', 'Issuing Authority'),
            'hukou' => Yii::t('app.c2', 'Hukou'),
            'work_unit' => Yii::t('app.c2', 'Work Unit'),
            'bound_vehicle' => Yii::t('app.c2', 'Bound Vehicle'),
        ];
    }

    /**
     * @inheritdoc
     * @return \common\models\c2\query\ProprietorQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \common\models\c2\query\ProprietorQuery(get_called_class());
    }

    public function behaviors()
    {
        return []; // TODO: Change the autogenerated stub
    }

    /**
    * setup default values
    **/
    public function loadDefaultValues($skipIfSet = true) {
        parent::loadDefaultValues($skipIfSet);
    }

    public function beforeSave($insert)
    {
        if ($this->isNewRecord) {
            $uid = Yii::$app->request->get('uid');
            $user = FeUser::findOne($uid);
            if (!$user) {
                return false;
            }
        }

        $data = [
            // 'id' => $this->id2,
            'community_id' => $this->community_id,
            'building_number_id' => $this->building_number_id,
            'unit_id' => $this->unit_id,
            'houses_id' => $this->houses_id,
            'name' => $this->name,
            'phone' => $this->phone,
            'sy_validity_start' => $this->sy_validity_start,
            'sy_validity_end' => $this->sy_validity_end,
            'remarks' => $this->remarks,
            'gender' => $this->gender,
            'country' => $this->country,
            'nation' => $this->nation,
            'birthday' => $this->birthday,
            'id_type' => $this->id_type,
            'id_number' => $this->id_number,
            'validity_start' => $this->validity_start,
            'validity_end' => $this->validity_end,
            'id_address' => $this->id_address,
            'Issuing_authority' => $this->Issuing_authority,
            'hukou' => $this->hukou,
            'work_unit' => $this->work_unit,
            'bound_vehicle' => $this->bound_vehicle,
        ];

        if ($this->isNewRecord) {
            $url = \Yii::$app->params['api_url'] . '/api/Building/SetAddProprietor?supplier_id=' . Yii::$app->params['supplier_id'];
        } else {
            $data['id'] = $this->id2;
            $url = \Yii::$app->params['api_url'] . '/api/Building/SetUpdateProprietor?supplier_id=' . Yii::$app->params['supplier_id'];
        }

        // 同步到第三方平台
        $data = Json::encode($data);
        $result = Tool::curlPost($url, $data);
        Yii::info($result);

        $result = json_decode($result, true);
        if ($result['Status'] && $result['Status'] == 1) {
            if ($this->isNewRecord) {
                $this->id2 = $result['Ext'];
                $user->updateAttributes([
                    'password_hash' => Yii::$app->security->generatePasswordHash('123456'),
                    'proprietor_id' => $this->id2,
                    'community_id' => $this->community_id,
                    'building_number_id' => $this->building_number_id,
                    'unit_id' => $this->unit_id,
                    'house_id' => $this->houses_id,
                    'status' => FeUser::STATUS_UN_AUDIT,
                ]);
                // todo 发送短信通知?
            }
            return parent::beforeSave($insert); // TODO: Change the autogenerated stub
        }

        Yii::$app->session->setFlash($this->getMessageName(), $result['Msg']);
        return false;
    }

}
